Skip to main content

sed

The sed command, which stands for "stream editor," is a powerful and versatile text manipulation tool used in Unix-like operating systems, including Linux. It is primarily used for performing text transformations on input text, such as files or pipelines of text, and it is often used for the following purposes:

  1. Search and Replace: One of the most common uses of sed is to search for a specific pattern or text in a file and replace it with another pattern or text. This is particularly useful for making bulk changes to text files.

    Example:

    sed 's/old-text/new-text/g' input.txt > output.txt
  2. Text Filtering: sed can be used to filter lines from input text based on a given condition or pattern. It allows you to extract specific lines from a file that match certain criteria.

    Example:

    sed -n '/pattern/p' input.txt
  3. Text Insertion and Appending: You can use sed to add or append text to specific lines in a file.

    Example:

    sed '/pattern/a\This line will be appended.' input.txt
  4. Text Deletion: sed can be used to delete specific lines or patterns from a file.

    Example:

    sed '/pattern/d' input.txt

    removing line form 1 to 10 of a file input.txt

    sed '1,10d' input.txt
  5. Text Transformation: You can perform more complex text transformations, such as converting text to uppercase or lowercase, reordering fields, and more.

    Example (converting text to uppercase):

    sed 's/.*/\U&/' input.txt
  6. Batch Processing: sed is useful for batch processing text files, making it a valuable tool for scripting and automation.

    Example (applying multiple sed commands in sequence):

    sed -e 's/foo/bar/' -e 's/123/456/' input.txt
  7. Regular Expressions: sed supports powerful regular expressions for pattern matching, making it suitable for complex text manipulation tasks.

    Example (using regular expressions to match a pattern):

    sed '/[0-9][0-9]*/d' input.txt
  8. In-Place Editing: sed can edit a file in-place without the need to create a separate output file, which can be useful for modifying files directly.

    Example (editing a file in-place):

    sed -i 's/old-text/new-text/g' input.txt
  9. Pipelining: sed can be used in pipelines with other commands to perform complex text processing operations.

    Example (using sed in a pipeline):

    cat input.txt | sed 's/old-text/new-text/g' | grep 'pattern'

In summary, the sed command is an essential tool for text processing and manipulation in the Unix/Linux environment. It provides a wide range of capabilities for working with text data and is frequently used in scripts and command-line operations for tasks like editing configuration files, log analysis, and more.